package codeforces;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

import static java.lang.Math.abs;
import static java.lang.Math.max;

public class CF1528A {
    final int N = (int) (1e5 + 10);

    int t, n;
    int[] l = new int[N], r = new int[N];

    long[][] dp = new long[N][2];

    private class Edge {
        int to, next;
    }

    Edge[] e = new Edge[N << 1];
    int[] head = new int[N];
    int idx = 1;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer st = new StreamTokenizer(br);

    private void run() throws Exception {
        t = readInt();
        while (t-- > 0) {
            n = readInt();
            for (int i = 1; i <= n; i++) {
                l[i] = readInt();
                r[i] = readInt();
            }
            // 清理上一条用例的数据
            clear();

            for (int i = 0; i < n - 1; i++) {
                int u = readInt(), v = readInt();
                addEdge(u, v);
                addEdge(v, u);
            }

            dfs(1);
            System.out.println(max(dp[1][0], dp[1][1]));
        }
    }

    private void clear() {
        for (int i = 1; i < dp.length; i++) {
            Arrays.fill(dp[i], 0);
        }

        Arrays.fill(head, -1);
        idx = 1;
        for (int i = 0; i < e.length; i++) {
            if (e[i] == null) e[i] = new Edge();
            e[i].to = 0;
            e[i].next = 0;
        }
    }

    private void addEdge(int u, int v) {
        e[idx].to = v;
        e[idx].next = head[u];
        head[u] = idx++;
    }

    private void dfs(int u) {
        dfs(u, 0);
    }

    private void dfs(int u, int f) {
        for (int i = head[u]; i != -1; i = e[i].next) {
            int v = e[i].to;
            if (v != f) {
                dfs(v, u);
                dp[u][0] += max(dp[v][0] + abs(l[u] - l[v]), dp[v][1] + abs(l[u] - r[v]));
                dp[u][1] += max(dp[v][0] + abs(r[u] - l[v]), dp[v][1] + abs(r[u] - r[v]));
            }
        }
    }


    private long readLong() throws IOException {
        st.nextToken();
        return (long) st.nval;
    }

    private int readInt() throws IOException {
        st.nextToken();
        return (int) st.nval;
    }

    private String readString() throws Exception {
        st.nextToken();
        return st.sval;
    }

    public static void main(String[] args) throws Exception {
        new CF1528A().run();
    }
}
